//
//  BrokerDataMange.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/22.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "BrokerDataMange.h"
#import "API.h"
#import "CustomerModel.h"

#import "PremisesModel.h"
#import "StringTool.h"
#import "MBProgressHUD+MJ.h"
#import "NSString+sizeWithFont.h"
#import "UserModal.h"
#import "JSONKit.h"

@implementation BrokerDataMange
#pragma mark 获取经纪人信息
-(ResulteMod *)refeshDataBrokerInfoWithCustomerId:(NSInteger)customerId
{
    NSString *url = [NSString stringWithFormat:@"%@%ld",[API urlMake:brokerinfo],(long)customerId];
    
    ResulteMod *mod = [Connect doGetWithCaChe:url];
    
    NSDictionary *resDic = mod.resultDic;
    
    mod.resultCode = [NSString stringWithFormat:@"%@",resDic[@"code"]];
    if (![mod.resultCode isEqualToString:@"20003"])
    {
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
        return mod;
    }
    
    NSDictionary *mainDic = resDic[@"brokerInfoDto"];
    mod.dataMod = mainDic;
    
    return mod;
}

#pragma mark 客户列表信息
-(ResulteMod *)refeshDataConsumerListWithCustomerId:(NSInteger)customerId WithPageIndex:(NSInteger)pageIndex WithKeyWord:(NSString *)keyWord WithCustomerState:(NSInteger)state

{
    NSInteger perItem = 10;
    NSString *url;
    if(state == 1) //真实客户
    {
        url = [NSString stringWithFormat:@"%@%@/%zd/%zd/%zd",[API urlMake:consumerlist],keyWord,customerId,pageIndex,perItem];
    }else if (state == -1) //潜在客户
    {
        url = [NSString stringWithFormat:@"%@%@/%zd/%zd/%zd",[API urlMake:potentialconsumerlist],keyWord,customerId,pageIndex,perItem];
    }
    
    ResulteMod *resultMod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:nil];
    
    NSDictionary *resDic = resultMod.resultDic;
    resultMod.resultCode = [NSString stringWithFormat:@"%@",[resDic objectForKey:@"code"]];
    if (![resultMod.resultCode isEqualToString:@"20003"] && ![resultMod.resultCode isEqualToString:@"0"]) {
        resultMod.resltState = ResultFail;
    }else
    {
        
        NSDictionary *resDic = resultMod.resultDic[@"model"];
        if ([resDic isKindOfClass:[NSNull class]] || resDic ==nil)
        {
            resultMod.dataMod = nil;
        }else
        {
            NSArray  *resArr = [NSArray arrayWithArray:resDic[@"consumerDtos"]];
            NSMutableArray *mainArr = [NSMutableArray array];
            for (NSDictionary  * dict in resArr)
            {
                CustomerModel *model = [CustomerModel customerModelWithDic:dict];
                
                [mainArr addObject:model];
            }
            resultMod.dataMod = mainArr;
        }
    }
    return resultMod;
}



#pragma mark 绑定楼盘

-(ResulteMod *)bindpremisesWithCustomerId:(NSString *)customerId WithPremisesIds:(NSString *)premisesIds
{
    NSString *url = [NSString stringWithFormat:@"%@",[API urlMake:bindpremises]];
    
    NSDictionary *dict = @{@"consumerId":customerId,@"premisesIds":premisesIds};
    
    ResulteMod *resultMod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    
    
    resultMod.resultCode = [NSString stringWithFormat:@"%@",resultMod.resultDic[@"code"]];
    if(![resultMod.resultCode  isEqual:@"20003"])
    {
        resultMod.resltState = ResultFail;
        resultMod.resultTip  = ![StringTool isBlankString:resultMod.resultDic[@"message"]]?resultMod.resultDic[@"message"]:[StringTool isBlankString:resultMod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",resultMod.resultCode];
    }
    return resultMod;

}

#pragma mark //绑定楼盘
-(void)bindpremisesWithCustomerId:(NSString *)customerId WithPremisesIds:(NSString *)premisesIds withCallBack:(FinishCallBack)block;
{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString *url = [NSString stringWithFormat:@"%@",[API urlMake:bindpremises]];
        
        NSDictionary *dict = @{@"consumerId":customerId,@"premisesIds":premisesIds};
        
        [Connect doPostWithCaCheWithUrl:url withFormdata:dict withCallBack:^(ResulteMod *resultMod) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                block(resultMod);
            });

            
        }];
    });
}
#pragma mark 获取楼盘列表
-(ResulteMod *)premiseslistWithUserCode:(NSString *)userCode;
{
    NSString *url = [NSString stringWithFormat:@"%@%@",[API urlMake:premiseslist],userCode];
    ResulteMod *mod = [Connect doGetWithOutCaChe:url];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    mod.resultTip = [NSString stringWithFormat:@"%@",mod.resultDic[@"message"]];
    
    if (![mod.resultCode isEqualToString:@"20003"]) {
        mod.resltState = ResultFail;
        if ([StringTool isBlankString:mod.resultTip]) {
            mod.resultTip = [NSString stringWithFormat:@"网络错误%@",mod.resultCode];
        }
        return mod;
    }
    
    NSArray *resArr = [[NSArray alloc]initWithArray:mod.resultDic[@"premisesDtos"]];
    NSMutableArray *mainArrM = [NSMutableArray array];
    for (NSDictionary *dict in resArr) {
        PremisesModel *model = [PremisesModel PremisesWithDict:dict];
        [mainArrM addObject:model];
    }
    mod.dataMod = mainArrM;
    return mod;
    
}

#pragma mark 修改客户状态(单个启用禁用）
-(ResulteMod *)modifyconsumerstatusWithBrokerId:(NSString *)brokderId withcConsumerId:(NSString *)constomerId withStatus:(NSString *)stauts;
{
    NSString *url = [NSString stringWithFormat:@"%@%@/%@/%@",[API urlMake:modifyconsumerstatus],brokderId,constomerId,stauts];
    ResulteMod *mod = [Connect doGetWithOutCaChe:url];
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
        return mod;
    }
    mod.resltState = ResultSuccess;
    return mod;
}

#pragma mark 转为真实客户）
-(ResulteMod *)potentialchangtorealWithConsumerId:(NSString *)consumerId withPotentialId:(NSString *)potentialId
{
    NSString *url = [NSString stringWithFormat:@"%@%@/%@",[API urlMake:potentialchangtoreal],consumerId,potentialId];
    
    ResulteMod *mod = [Connect doGetWithOutCaChe:url];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
    }
    
    return mod;
}

#pragma mark 锁定客户（潜在客户）
-(ResulteMod *)lockpotentiallWithConsumerId:(NSString *)consumerId withPotentialId:(NSString *)potentialId
{
    NSString *url = [NSString stringWithFormat:@"%@%@/%@",[API urlMake:lockpotential],consumerId,potentialId];
    ResulteMod *mod = [Connect doGetWithOutCaChe:url];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }
    
    return mod;
}


#pragma  mark 添加真实客户
-(ResulteMod *)addconsumerWithCustmerModel:(AddRealCusModel *)model withCustomerId:(NSString *)customerId withpotentialId:(NSString *)potentialId;
{
    NSString *url = [NSString stringWithFormat:@"%@%@",[API urlMake:addconsumer],customerId];
   
    NSString *password =  [StringTool isBlankString:model.password]?@"":[NSString md5:model.password];
    NSString *repassword = [StringTool isBlankString:model.repassword]?@"":[NSString md5:model.repassword];
    NSDictionary *dict = @{@"name":model.name,@"mobile":model.mobile,@"captcha":@"123456",@"password":password,@"repassword":repassword,@"premisesIds":model.premisesIds,@"potentialId":potentialId};
    
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
        if ([mod.resultCode isEqualToString:@"30002"]) {
            mod.resultTip = @"验证码错误";
        }else
        {
             mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
        }
       
    }
    
    return mod;

}

#pragma mark 添加潜在客户
-(ResulteMod *)ddpotentialconsumerWithPotentialModel:(AddLatentCusModel *)model withcustomerId:(NSString *)customerId;
{
    NSString *url = [NSString stringWithFormat:@"%@%@",[API urlMake:ddpotentialconsumer],customerId];
    NSDictionary *dict = @{@"name":model.name,@"mobile":model.mobile,@"idCard":model.idCard,@"memo":model.memo,@"sex":model.sex,@"idCardType":model.idCardType};
    
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
    }
    
    return mod;
}



#pragma mark 编辑潜在客户
-(ResulteMod *)updatepotentialconsumerWithPotentialModel:(AddLatentCusModel *)model withcustomerId:(NSString *)customerId;
{
    NSString *url = [NSString stringWithFormat:@"%@%zd",[API urlMake:updatepotentialconsumer],[UserModal shareUserModel].userId];
    NSDictionary *dict = @{@"name":model.name,@"mobile":model.mobile,@"idCard":model.idCard,@"memo":model.memo,@"sex":model.sex,@"idCardType":model.idCardType,@"id":customerId};
    
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
    }
    
    return mod;

}

#pragma mark 编辑真实客户
-(ResulteMod *)updateconsumerWithCustmerModel:(AddRealCusModel *)model withCustomerId:(NSString *)customerId WithPremiseIds:(NSString *)prmiseIds;
{
    NSString *url = [NSString stringWithFormat:@"%@%zd",[API urlMake:updateconsumer],[UserModal shareUserModel].userId];
    
    NSString *password =  [StringTool isBlankString:model.password]?@"":[NSString md5:model.password];
    NSString *repassword = [StringTool isBlankString:model.repassword]?@"":[NSString md5:model.repassword];
    
    NSLog(@"绑定楼盘=== %@",[UserModal shareUserModel].premiseIds);
    NSDictionary *dict = @{@"name":model.name,@"mobile":model.mobile,@"captcha":model.captch,@"password":password,@"repassword":repassword,@"premisesIds":prmiseIds,@"id":customerId};
    
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }
    
    return mod;

}

#pragma mark 批量操作（启用，禁用） custoemrIds:被操作的客户，customerId：当前用户ID staus:0:表示要启用操作，1禁用操作
-(ResulteMod *)batchmodifystatusWithCustoemrIds:(NSString *)custoemrIds WithcustomerId:(NSString *)customerId WithStatus:(NSString *)stauts;
{
    NSString *url = [NSString stringWithFormat:@"%@",[API urlMake:batchmodifystatus]];
    NSDictionary *dict = @{@"consumerId":customerId,@"customerIds":custoemrIds,@"status":stauts};
    
    ResulteMod *mod= [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    mod.resultTip = mod.resultDic[@"message"];
    
    if (![mod.resultCode isEqualToString:@"20003"]) {
       mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }
    return mod;
}

#pragma mark 线下下单申请
-(ResulteMod *)orderapplyWithOrderModel:(OrderApplayModel *)model;
{
    NSString *url = [NSString stringWithFormat:@"%@%zd",[API urlMake:orderapply],[UserModal shareUserModel].userId];
    
    NSDictionary *dict = @{@"csmName":model.csmName,@"csmPhone":model.csmPhone,@"csmIdCard":model.csmIdCard,@"housingBusinessNo":model.housingBusinessNo,@"premisesBusinessNo":model.premisesBusinessNo,@"bank":model.bank,@"bankCardName":model.bankCardName,@"bankCardId":model.bankCardId,@"amount":model.amount,@"remarks":model.remarks};
    
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    mod.resultTip = mod.resultDic[@"message"];
    
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }
    return mod;
}


#pragma mark 添加真实客户获取验证码
-(ResulteMod *)addconsumercaptcha:(NSString *)mobile;
{
    NSString *url = [NSString stringWithFormat:@"%@%@/%zd",[API urlMake:addconsumercaptcha1],mobile,[UserModal shareUserModel].userId];
    ResulteMod *mod = [Connect doGetWithOutCaChe:url];
    NSDictionary *reDic = [mod.resultData objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",reDic[@"code"]];
    mod.resultTip = reDic[@"message"];
    
    if (![mod.resultCode isEqualToString:@"1001"]) {
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
        
    }
    
    return mod;
    
}


@end
